import { request, requestNoParseJson } from '../utils/request';
import { api } from '../utils/config';
import { getLocalStorage } from '../utils/storage';

const { registerUrl, loginUrl, submitPhysicianAuthUrl,
  submitImageUrl, deptUrl } = api;

const { registerDoctorService, submitQualificationsUrl, departmentListUrl, doctorLevelListUrl,
   registerCodeUrl, uploadTokenUrl } = api;

export async function register(data) {
  return requestNoParseJson(registerUrl, {
    method: 'post',
    headers: {
      'content-type': 'application/json',
    },
    body: JSON.stringify(data),
  });
}
export async function registerDoctor(data) {
  return requestNoParseJson(registerDoctorService, {
    method: 'post',
    headers: {
      'content-type': 'application/json',
    },
    body: JSON.stringify(data),
  });
}

export async function login(data) {
  return request(loginUrl, {
    method: 'post',
    headers: {
      'content-type': 'application/json',
    },
    body: JSON.stringify(data),
  });
}

export async function submitPhysicianAuth(data) {
  const token = getLocalStorage('access_token');
  return request(submitPhysicianAuthUrl, {
    method: 'post',
    headers: {
      'content-type': 'application/json',
      Authorization: `Bearer ${token}`,
    },
    body: JSON.stringify(data),
  });
}

export async function submitImage(data) {
  const token = getLocalStorage('access_token');
  return requestNoParseJson(submitImageUrl, {
    method: 'post',
    headers: {
      'content-type': 'application/json',
      Authorization: `Bearer ${token}`,
    },
    body: JSON.stringify(data),
  });
}

export async function submitQualificationns(data) {
  const token = getLocalStorage('access_token');
  return requestNoParseJson(submitQualificationsUrl, {
    method: 'post',
    headers: {
      'content-type': 'application/json',
      Authorization: `Bearer ${token}`,
    },
    body: JSON.stringify(data),
  });
}

export async function queryDepartmentList(data) {
  return request(departmentListUrl, {
    method: 'post',
    headers: {
      'content-type': 'application/json',
    },
    body: JSON.stringify(data),
  });
}
export async function queryDept(data) {
  return request(deptUrl, {
    method: 'post',
    headers: {
      'content-type': 'application/json',
    },
    body: JSON.stringify(data),
  });
}

export async function queryDoctorLevelList(data) {
  return request(doctorLevelListUrl, {
    method: 'post',
    headers: {
      'content-type': 'application/json',
    },
    body: JSON.stringify(data),
  });
}

export async function getValidCode(data) {
  return requestNoParseJson(registerCodeUrl, {
    method: 'post',
    headers: {
      'content-type': 'application/json',
    },
    body: JSON.stringify(data),
  });
}

/**
 * 获取七牛上传TOKEN
 */
export async function getUploadToken(params) {
  return request(uploadTokenUrl, {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${params.token}`,
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      bucket: params.bucket,
      insertOnly: params.insertOnly,
    }),
  });
}
